package com.yhy.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yhy.autoconfig.templates.HuanXinTemplate;
import com.yhy.domain.Announcement;
import com.yhy.domain.UserInfo;
import com.yhy.domain.mongo.Comment;
import com.yhy.domain.mongo.Friend;
import com.yhy.domain.vo.AnnouncementVo;
import com.yhy.domain.vo.ContactVo;
import com.yhy.domain.vo.MessageLike;
import com.yhy.domain.vo.PageResult;
import com.yhy.dubbo.api.MessageApi;


import com.yhy.dubbo.api.UserInfoApi;
import com.yhy.dubbo.api.mongo.CommentApi;
import com.yhy.dubbo.api.mongo.FriendApi;
import com.yhy.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageService {

    @Reference
    private MessageApi messageApi;

    @Reference
    private FriendApi friendApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private CommentApi commentApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    public ResponseEntity announcements(Integer page, Integer pagesize) {
        IPage<AnnouncementVo> iPage = messageApi.findByPage(page,pagesize);
        PageResult pageResult = new PageResult(page,pagesize, (int) iPage.getTotal(),iPage.getRecords());
        List<AnnouncementVo> records = iPage.getRecords();
        System.err.println(records);

        return ResponseEntity.ok(pageResult);
    }


    public ResponseEntity addContacts(Long friendUserId) {

        friendApi.addContacts(UserHolder.getUserId(),friendUserId);

        huanXinTemplate.contactUsers(UserHolder.getUserId(),friendUserId);

        return ResponseEntity.ok(null);
    }

    public ResponseEntity queryContactsList(Integer page, Integer pagesize) {
        //1.获取当前userId
        Long userId = UserHolder.getUserId();
        //2.调用api查询包含List<Friend>的pageResult
        PageResult pageResult = friendApi.findAllFriend(page,pagesize,userId);
        List<Friend> items = (List<Friend>) pageResult.getItems();

//        3.封装List<ContactVo> 替换原有List<Friend>
        List<ContactVo> list = new ArrayList<>();
        for (Friend item : items) {
            UserInfo userInfo = userInfoApi.findById(item.getFriendId());
            ContactVo contactVo = ContactVo.init(userInfo);
            list.add(contactVo);
        }
        pageResult.setItems(list);
        return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity likes(Integer page, Integer pagesize, Integer commentType) {
        //1.获取当前用户id
        Long publishUserId = UserHolder.getUserId();
        //2.调用api查询出pageResult<Comment>
        PageResult pageResult = commentApi.findByUidAndType(page,pagesize,publishUserId,commentType);
        //3.是用List<Comment>里的数据调用api封装vo对象
        List<Comment> items = (List<Comment>) pageResult.getItems();

        System.err.println(items);
        List<MessageLike> list = new ArrayList<>();
        for (Comment item : items) {
            UserInfo userInfo = userInfoApi.findById(item.getUserId());
            System.out.println("userinfo==="+userInfo);
            MessageLike vo = MessageLike.init(userInfo, item.getCreated());
            list.add(vo);
        }
        System.out.println(list);
        //4.替换pageResult里的List
        pageResult.setItems(list);
        return ResponseEntity.ok(pageResult);
    }
}
